package com.autonavi.yunda.yunji.service;

import com.alibaba.buc.sso.client.util.SimpleUserUtil;
import com.alibaba.buc.sso.client.vo.BucSSOUser;
import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author cizhong.wcs
 * @date 2023/5/12 13:52
 */
@Slf4j
@Component
public class BucUserService {
    @Autowired
    private HttpServletRequest request;

    public String getCurEmployeeId() {
        try {
            return SimpleUserUtil.getBucSSOUser(request).getEmpId();
        } catch (Exception e) {
            throw new AmapException(AmapExceptionCode.EXECUTION_LINK_EXECUTE_ERROR, "获取BUC登录用户异常", e);
        }
    }

    public String getCurEmployeeIdOrNull() {
        try {
            BucSSOUser bucSSOUser = SimpleUserUtil.getBucSSOUser(request);
            if (bucSSOUser == null) {
                log.error("getUserId warn, bucSSOUser is null");
                return null;
            }
            return bucSSOUser.getEmpId();
        } catch (Exception e) {
            log.error("getUser error", e);
            return null;
        }
    }
}
